package com.dddpeter.app.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.dddpeter.app.entity.Comment;
import com.dddpeter.app.entity.CommentLike;
import com.dddpeter.app.repository.CommentMapper;
import com.dddpeter.app.repository.CommentLikeMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Random;

/**
 * 评论服务类
 */
@Service
public class CommentService {
    
    @Autowired
    private CommentMapper commentMapper;
    
    @Autowired
    private CommentLikeMapper commentLikeMapper;
    
    // 随机昵称和头像
    private final String[] NICKNAMES = {
        "匿名用户", "访客", "网友", "路人甲", "吃瓜群众", "程序员", "开发者", 
        "技术达人", "代码爱好者", "学习中的小白", "菜鸟程序员", "资深码农"
    };
    
    private final String[] AVATARS = {
        "https://api.dicebear.com/7.x/avataaars/svg?seed=1",
        "https://api.dicebear.com/7.x/avataaars/svg?seed=2",
        "https://api.dicebear.com/7.x/avataaars/svg?seed=3",
        "https://api.dicebear.com/7.x/avataaars/svg?seed=4",
        "https://api.dicebear.com/7.x/avataaars/svg?seed=5",
        "https://api.dicebear.com/7.x/avataaars/svg?seed=6",
        "https://api.dicebear.com/7.x/avataaars/svg?seed=7",
        "https://api.dicebear.com/7.x/avataaars/svg?seed=8"
    };
    
    /**
     * 获取文章评论列表
     */
    public List<Comment> getCommentsByArticleId(Long articleId) {
        List<Comment> comments = commentMapper.findCommentsByArticleId(articleId);
        
        // 为每个评论加载回复
        for (Comment comment : comments) {
            List<Comment> replies = commentMapper.findRepliesByParentId(comment.getId());
            comment.setReplies(replies);
        }
        
        return comments;
    }
    
    /**
     * 添加评论
     */
    @Transactional
    public Comment addComment(Comment comment) {
        // 生成随机昵称和头像
        if (comment.getIsAnonymous()) {
            Random random = new Random();
            comment.setNickname(NICKNAMES[random.nextInt(NICKNAMES.length)]);
            comment.setAvatar(AVATARS[random.nextInt(AVATARS.length)]);
        }
        
        comment.setCreatedAt(LocalDateTime.now());
        comment.setUpdatedAt(LocalDateTime.now());
        
        commentMapper.insert(comment);
        return comment;
    }
    
    /**
     * 点赞评论
     */
    @Transactional
    public boolean likeComment(Long commentId, String nickname) {
        // 检查是否已经点赞
        int exists = commentLikeMapper.checkLikeExists(commentId, nickname);
        if (exists > 0) {
            return false; // 已经点赞过了
        }
        
        // 添加点赞记录
        CommentLike like = new CommentLike();
        like.setCommentId(commentId);
        like.setNickname(nickname);
        like.setCreatedAt(LocalDateTime.now());
        commentLikeMapper.insert(like);
        
        // 更新评论点赞数
        Comment comment = commentMapper.selectById(commentId);
        if (comment != null) {
            comment.setLikeCount(comment.getLikeCount() + 1);
            commentMapper.updateById(comment);
        }
        
        return true;
    }
    
    /**
     * 检查用户是否已点赞评论
     */
    public boolean hasLikedComment(Long commentId, String nickname) {
        return commentLikeMapper.checkLikeExists(commentId, nickname) > 0;
    }
}
